Part 3

Row

Accidents Map

Accidents by Gender

---
title: "Motor Vehicle Accidents in Victoria"
output: 
  flexdashboard::flex_dashboard:
        vertical_layout: scroll
        orientation: rows
        source_code: embed
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
                      eval = TRUE,
                      message = FALSE, 
                      warning = FALSE)
```

```{r libraries}
library(flexdashboard)
library(tidyverse)
library(lubridate)
library(janitor)
library(knitr)
library(kableExtra)
library(ggmap)
library(ggthemes)
library(plotly)
```

```{r load-data}
accidents <- read_csv("data/ACCIDENT.csv") %>%
  clean_names()

locations <- read_csv("data/ACCIDENT_LOCATION.csv") %>%
  clean_names()

nodes <- read_csv("data/NODE.csv") %>%
  clean_names()

persons <- read_csv("data/PERSON.csv") %>%
  clean_names()

vehicles <- read_csv("data/VEHICLE.csv") %>%
  clean_names()
```

```{r create-year-hour-day}
accidents <- accidents %>%
  mutate(accidentdate = dmy(accidentdate),
         Year = year(accidentdate),
         Hour = hour(accidenttime),
         Weekday = wday(accidentdate, 
                        label = TRUE,
                        abbr = FALSE))
```

Part 3 {data-icon="fa-battery-three-quarters"}
===================================== 

Row {.tabset data-height=800}
---------

### **Accidents Map**

```{r accidents-map}
location_box <- c(min(nodes$long), 
                  min(nodes$lat), 
                  max(nodes$long),
                  max(nodes$lat))

location_map <- get_map(location = location_box, 
                        source = 'osm')

  ggmap(location_map) + 
    geom_point(data = nodes,
               aes(x = long,
                   y = lat), 
               colour="#0072B2", 
               alpha=0.5, 
               size = 0.05) +
    theme_map()
```

### **Accidents by Gender**

```{r accidents-gender-table}
persons <- persons %>%
  mutate(sex = recode(sex,
                      "F" = "Female",
                      "M" = "Male",
                      "U" = "Unknown"))
```

```{r accidents-gender-plot}
persons %>% 
  filter(road_user_type_desc == "Drivers",
         sex %in% c("Female",
                    "Male"),
         age > 15) %>% 
  count(age,
        sex,
        name = "Accidents") %>% 
  
  ggplot(aes(x = age,
             y = Accidents, 
             color = sex)) +
  geom_line() +
  xlab("Age")
  
ggplotly()
```